<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Db;

class Index extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
    public function kemu()
    {
        return $this->view->fetch();
    }
    public function indexold()
    {
        return $this->view->fetch();
    }


    ///////////////// 微信公众号授权登录
    public function authWechatOfficeAutoLogin(){
        if ($this->request->has("token")){
            cookie("token",$this->request->param("token"));
        }
        $options = array(
            'token'=>'uctoo', //填写你设定的key
            'appid'=> get_addon_config('epay')['wechat']['app_id'], //填写高级调用功能的app id, 请在微信开发模式后台查询
            'appsecret'=> get_addon_config('epay')['wechat']['app_secret'], //填写高级调用功能的密钥
            'encodingaeskey'=>'8AR1O5sz6t57uevKJzDfmcZyR7EessBhXd1mlq1Rxo4', //填写高级调用功能的密钥
        );
        $wechatAuth = new Wxauth($options);
        $result = $wechatAuth->wxuser;
        if (count($result) > 0){
            $token = cookie('token');
            $data = Token::get($token);
            $userInfo = \app\common\model\User::get($data['user_id']);
            $userInfo->wx_union_id = $result['unionid'];
            $userInfo->nickname = $result['nickname'];
            $userInfo->avatar = $result['avatar'];
            $userInfo->save();
            $this->redirect("https://vip.niukewang.com/m/pages/my/index");
        }
    }
    #创建菜单
    public function createMenu(){
        $options = array(
            'appid'=> get_addon_config('epay')['wechat']['app_id'], //填写高级调用功能的app id, 请在微信开发模式后台查询
            'appsecret'=> get_addon_config('epay')['wechat']['app_secret'], //填写高级调用功能的密钥
            'encodingaeskey'=>'8AR1O5sz6t57uevKJzDfmcZyR7EessBhXd1mlq1Rxo4', //填写高级调用功能的密钥
        );
        $wechat = new Wechat($options);
        $menu = [
            'button' => [
                [
                    'type' => "view",
                    'name' => "套餐",
                    'url' => 'https://vip.niukewang.com/m/'
                ]
            ]
        ];
        $token = $wechat->getAccessToken();
        $result = $wechat->createMenu($menu);
        halt($result);
    }
}
